package com.yunhe.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yunhe.bean.Admin;
import com.yunhe.bean.ShopUser;
import com.yunhe.service.adminLoginService;
import com.yunhe.service.checkUserService;

/**
 * Servlet implementation class adminLogin
 */
@WebServlet("/hou/adminLogin.do")
public class adminLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public adminLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username=request.getParameter("username");
		
		String userpass=request.getParameter("password");
	
		String co=request.getParameter("jilu");
		if(co!=null){
			String login=username+","+userpass;
			Cookie c = new Cookie("login", login);
			c.setMaxAge(60*5);
			response.addCookie(c);			
			}
		Admin admin1=null;
		Admin admin = new Admin();
		admin.setAname(username);
		admin.setApass(userpass);
		int a=0;
		List<Admin> checklogin = new adminLoginService().checklogin(admin);
		
		
		if(checklogin.size()>0){

			for (Admin Admin : checklogin) {
				System.out.println(Admin);
				a=Admin.getAid();
				admin1=Admin;
		}
		HttpSession session  = request.getSession();		
		session.setAttribute("sessionadmin", admin1);
		session.setAttribute("adminid",a);
		response.sendRedirect("index.jsp");
		/*request.getRequestDispatcher("succLogin.jsp").forward(request, response);*/
		}else{
			response.sendRedirect("login.jsp");
		}
	}

}
